Sample URL rewrite servlet
This appendix describes how to set up an example to test session management using URL rewrites. The class SessionSampleURLRewrite has been provided since none of the standard samples use URL rewriting to manage sessions. This is because using URL rewrites requires additions to the normal servlet code.
public class SessionSampleURLRewrite extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Step 1: Get the Session object boolean create = true; HttpSession session = request.getSession(create); // Step 2: Get the session data value Integer ival = (Integer) session.getAttribute ("sessiontest.counter"); if (ival == null) ival = new Integer (1); else ival = new Integer (ival.intValue () + 1); session.setAttribute ("sessiontest.counter", ival); // Step 3: Rewrite the session ID onto the URL String contextPath = request.getContextPath(); String servletName = request.getServletPath(); String encodeString = contextPath + servletName; String encodedURL = response.encodeURL(encodeString); // Step 4: Output the page response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Session Tracking Test</title></head>"); out.println("<body>"); out.println("<h1>Session Tracking Test</h1>"); out.println ("You have hit this page " + ival + " times" + "<br>"); if (!session.isNew()) { out.println("<p>"); if(request.isRequestedSessionIdFromURL()) out.print("Your requested session ID was found in a rewritten URL"); else out.print("Your requested session ID was NOT found in a rewritten URL"); } // Use the rewritten URL as the link, You must enable URL Rewriting in the session Manager and // disable cookies in either the Session Manager or the browser out.print("<p>"); out.print("<a href=\""); out.print(encodedURL); out.println("\">Request this servlet again using the rewritten URL</a>"); out.println("</body></html>"); } }
Steps to install SessionSampleURLRewrite servlet
We provide two alternatives to install the SessionSampleURLRewrite servlet:
Installing the urltest Web module
Adding the servlet to an installed application
Installing the urltest Web module
Please refer to 7.6.2, Install BeenThere on detailed instructions about how to install an enterprise application. In summary, these are the steps to install urltest.war:
1. Locate urltest.war and store it on your workstation or on your server. 2. Open the WebSphere Administrative Console and select Application -> New Application. 3. In the Preparing for the application installation window: a. Browse to the urltest.war Web module archive (Local path or Server path). b. Enter a context root, such as /urltest. c. Click Next. 4. Leave all defaults on the Preparing for the application installation window and click Next again. 5. If desired, change the name of the application (default = urltest_war) in Step 1 of the Install New Application procedure. Click Next. 6. Map the Web Module urltest to the desired Virtual Host. Clicking Next brings you to Step 3. 7. On the Step 3: Map modules to appservers window, from the Clusters and Servers selection box choose the application server you wish to install urltest.war on. Then check the box for the module urltest and click Apply. Click Next once again. 8. Verify the installation on the Step 4: Summary window and click Finish to install the urltest Web module. 9. Save the changes to the master configuration. 10. To start the urltest_war enterprise application, locate urltest_war in the Enterprise Applications view. Select the application and click Start. 11. Regenerate the plug-in configuration: Select Environment -> Update Web Server Plug-in. Click the OK button to update the plug-in configuration file. 12. Change the path information in <WAS_HOME>/config/plugin-cfg.xml file, then copy it to your remote Web servers (if needed, refer to 7.6.3, Regenerate Web server plug-in for instructions on how to do so).
Adding the servlet to an installed application
Note: While convenient in a development environment, the following technique is not recommended in a production environment.
Follow these steps to install the servlet into the sample application. This procedure can be performed whether the appserver is running or not. If the application server is running, then the changes will be picked up automatically, subject to the auto-reload settings on your Web container. If auto-reload is disabled, the servlet will not be available until the application server is restarted.
This procedure should be repeated for each of the cluster members of the cluster you are using to test session management.
1. Compile or download the SessionSampleURLRewrite.class file. 2. Place the SessionSampleURLRewrite.class file in the directory: <WAS_HOME>/installedApps/<node_name>/DefaultApplication.ear/Default WebApplication.war/WEB_INF/classesFor example:
c:\websphere\appserver\installedApps\app1\DefaultApplication.ear\Default
WebApplication.war\WEB_INF\classes3. Open the web.xml file for editing within the directory: <WAS_HOME>/config/cells/<node_name>/applications/DefaultApplication.
ear/deployments/DefaultApplication/DefaultWebApplication.war/WEB-INF4. Locate the last servlet tag definition, just before the first servlet-mapping tag, and add the lines listed in Example B-2 at this point. Make sure that the IDs used follow the pattern in the file; for example the last servlet definition should be Servlet_3, so use Servlet_4 for your new servlet definition.
Example: B-2 Lines to add to web.xml file
<servlet id="Servlet_4"><servlet-name>urltest</servlet-name><description>Test URL rewrite</description><servlet-class>SessionSampleURLRewrite</servlet-class></servlet><servlet-mapping id="ServletMapping_4"><servlet-name>urltest</servlet-name><url-pattern>/servlet/urltest</url-pattern></servlet-mapping>
5. Save and close the file. 6. If the appserver is not running, start the application server. If it is already running but you have disabled auto-reload (See Auto-reload setting of your Web module), restart the application server. 7. Regenerate the plug-in configuration file for your Web server(s). See Manual regeneration of the plug-in file to see how to do this. 8. Open the regenerated plugin.cfg.xml file and verify that the URL we defined in step 4 has been entered. The following line should appear: <Uri Name="/servlet/urltest"/>Repeat these steps for each of the nodes in your server group. Once this is complete, you will be able to run the test shown in URL rewriting.
Locating the Web material
The Web material associated with this redbook is available in softcopy on the Internet from the IBM Redbooks Web server. Point your Web browser to:ftp://www.redbooks.ibm.com/redbooks/SG246198Alternatively, you can go to the IBM Redbooks Web site at:
ibm.com/redbooksSelect the Additional materials and open the directory that corresponds with the redbook form number, SG24-6198-01.
Using the Web material
The additional Web material that accompanies this redbook includes the following files:
File name >Description
urltest.war URL Rewrite Example Web module
morton0206.zip BeenThere application code
TradeRedirector.zip Trade3 back-end application for WebSphere MQ scenario
System requirements for downloading the Web material
The following system configuration is recommended:
Hard disk space: 10 MB
Operating System: Windows, AIX, Solaris, Linux, OS/400®
Processor: 500 MHz Pentium®, RS/6000®, iSeries, Sparc
Memory: 384 MB RAM minimum
How to use the Web material
Create a subdirectory (folder) on your workstation or your server, and download the urltest.war file into this folder. Create a subdirectory (folder) on a Windows system and download the morton0206.zip file into this folder. Extract the BeenThere50.ear file from the zip file and copy it either into the <install_root>/installableApps directory of your appserver or to a local folder on your workstation. This Enterprise Application Resource is all you need to install the BeenThere application on your applications server(s). Download TradeRedirector.zip. The zip-file consists of two files:
TradeRedirector.ear
Instructions for setup of TradeRedirector.txt Follow the install instructions from the text file.
WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.
IBM is a trademark of the IBM Corporation in the United States, other countries, or both.